package com.zxy.condition;

import com.zxy.bean.RainBow;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    /**
     *
     * @param importingClassMetadata  当前类的注解信息
     * @param registry          BeanDefinitionRegistry注册类
     *                          把所有需要添加到容器中的bean：调用
     *                          BeanDefinitionRegistry。registerBeanDefinition手工注册进来
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean definition = registry.containsBeanDefinition("com.zxy.bean.Red");
        boolean definition1 = registry.containsBeanDefinition("com.zxy.bean.Black");
        if (definition && definition1){
           //指定bean定义信息（bean的类型）
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(RainBow.class);
           //注册一个bean，指定bean名
            registry.registerBeanDefinition("RainBow", rootBeanDefinition);
        }
    }
}
